/*
 * Copyright (C) 2004, Velocityme Partnership
 * ContactDetailTypeActionForm.java
 *
 * Created on 16 October 2004, 10:04
 */

package com.velocityme.www.actionforms;

import com.velocityme.entity.ContactDetailType;
import com.velocityme.enums.NotificationMechanism;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ContactDetailTypeActionForm extends ActionForm {
    
    public class InternalContactDetailType implements Serializable {
        private Long m_contactDetailTypeId;
        private String m_typeName;
        private NotificationMechanism m_notificationMechanism;
        private boolean m_isShown = false;
        private boolean m_isShownInDirectory = false;

        public InternalContactDetailType() {
            m_notificationMechanism = NotificationMechanism.UNSUPPORTED;
        }
        
        public Long getContactDetailTypeId() { return m_contactDetailTypeId; }
        public void setContactDetailTypeId(Long contactDetailTypeId) { m_contactDetailTypeId = contactDetailTypeId; }
        public String getTypeName() { return m_typeName; }
        public void setTypeName(String typeName) { m_typeName = typeName; }
        public NotificationMechanism getNotificationMechanism() { return m_notificationMechanism; }
        public int getNotificationMechanismId() { return m_notificationMechanism.ordinal(); }
        public void setNotificationMechanismId(int ordinal) { m_notificationMechanism = NotificationMechanism.values()[ordinal]; }
        public boolean getIsShown() { return m_isShown; }
        public void setIsShown(boolean isShown) { m_isShown = isShown; }
        public boolean getIsShownInDirectory() { return m_isShownInDirectory; }
        public void setIsShownInDirectory(boolean isShownInDirectory) { m_isShownInDirectory = isShownInDirectory; }
    }
    
    private ArrayList<InternalContactDetailType> m_contactDetailTypeList;
    private int m_selectedValue = 0;
    private String m_cmd="";
    
    /** Creates a new instance of ContactDetailTypeActionForm */
    public ContactDetailTypeActionForm() {
        m_contactDetailTypeList = new ArrayList<InternalContactDetailType>();
    }

    @Override
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        for (InternalContactDetailType contactDetailType: m_contactDetailTypeList) {
            contactDetailType.setIsShown(false);
            contactDetailType.setIsShownInDirectory(false);
        }
    }
    
    public void setCmd(int index, String value) {
        m_selectedValue = index;
        m_cmd = value;
    }
    
    public String getCmd(int index) {
        return m_cmd;
    }
    
    public String getCmd() {
        return m_cmd;
    }
    
    public int getIndex() {
        return m_selectedValue;
    }
    
    public void setContactDetailTypes(Collection<ContactDetailType> contactDetailTypes) {
        m_contactDetailTypeList.clear();
        // we assume that the contact detail type values are already sorted by
        // sequence number!
        for (ContactDetailType contactDetailType: contactDetailTypes) {
            InternalContactDetailType internalContactDetailType = new InternalContactDetailType();
            internalContactDetailType.m_contactDetailTypeId = contactDetailType.getId();
            internalContactDetailType.m_typeName = contactDetailType.getName();
            internalContactDetailType.m_notificationMechanism = contactDetailType.getNotificationMechanism();
            internalContactDetailType.m_isShown = contactDetailType.isShowType();
            internalContactDetailType.m_isShownInDirectory = contactDetailType.isShowInDirectory();
            m_contactDetailTypeList.add(internalContactDetailType);
        }
    }
    
    public Collection<ContactDetailType> getContactDetailTypes() {
        Collection<ContactDetailType> typeValues = new ArrayList<ContactDetailType>();
        int sequenceNumber = 0;
        for (InternalContactDetailType contactDetailType: m_contactDetailTypeList) {
            ContactDetailType contactDetailTypeValue = new ContactDetailType(contactDetailType.getNotificationMechanism(),
                    contactDetailType.getTypeName(),
                    contactDetailType.getIsShown(),
                    contactDetailType.getIsShownInDirectory(),
                    sequenceNumber++);
            if (contactDetailType.getContactDetailTypeId() != null)
                contactDetailTypeValue.setId(contactDetailType.getContactDetailTypeId());
            typeValues.add(contactDetailTypeValue);
        }
        return typeValues;
    }
    
    public boolean processCmd() {
        if (m_cmd.equals("Insert")) {
            m_contactDetailTypeList.add(m_selectedValue, new InternalContactDetailType());
            return true;
        }
        if (m_cmd.equals("Delete")) {
            m_contactDetailTypeList.remove(m_selectedValue);
            return true;
        }
        if (m_cmd.equals("Move Up")) {
            if (m_selectedValue > 0) {
                InternalContactDetailType obj = m_contactDetailTypeList.remove(m_selectedValue-1);
                m_contactDetailTypeList.add(m_selectedValue, obj);
            }
            return true;
        }
        return false;
    }
    
    public ArrayList getContactDetailTypeList() {
        return m_contactDetailTypeList;
    }
    
    public void setContactDetailTypeList(ArrayList contactDetailTypeList) {
        m_contactDetailTypeList = contactDetailTypeList;
    }
    
}
